//
//  YZBaseLaunchVC.m
//  VET
//
//  Created by Lam BG on 2018/7/2.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZBaseLaunchVC.h"

#import "YZStateBtn.h"

#import "YZStatementHeader.h"

#import "UIColor+Category.h"

@interface YZBaseLaunchVC () 

@property (weak, nonatomic) UITextField *numberTF;
@property (weak, nonatomic) UIImageView *numberInfoIV;
@property (weak, nonatomic) UIView *numberView;

@property (weak, nonatomic) UITextField *passwordTF;
@property (weak, nonatomic) YZStateBtn *passwordStateBtn;
@property (weak, nonatomic) UIView *passwordView;

@property (weak, nonatomic) UITextField *verCodeTF;
@property (weak, nonatomic) UIView *verCodeView;

@end

@implementation YZBaseLaunchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setup];
    [self bindVM];
}

#pragma mark - private function

- (void)setup {
    
    // 键盘监听
    self.numberTF.delegate = self;
    self.passwordTF.delegate = self;
    self.verCodeTF.delegate = self;
    // 密码
    self.passwordTF.secureTextEntry = YES;
    if (@available(iOS 10.0, *)) {
        self.passwordTF.textContentType = UITextContentTypeFullStreetAddress;
    } else {
        // Fallback on earlier versions
    }
}

- (void)bindVM {}

#pragma mark - text field delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    if (self.numberTF == textField) {
        
        self.numberView.backgroundColor = yzGreenColor;
    }
    
    if (self.passwordTF == textField) {
        
        self.passwordView.backgroundColor = yzGreenColor;
        
        if (textField.secureTextEntry) {
            
            [textField insertText:self.passwordTF.text];
        }
    }
    
    if (self.verCodeTF == textField) {
        
        self.verCodeView.backgroundColor = yzGreenColor;
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (self.numberTF == textField) {
        
        self.numberView.backgroundColor = self.grayColor;
    }
    
    if (self.passwordTF == textField) {
        
        self.passwordView.backgroundColor = self.grayColor;
    }
    
    if (self.verCodeTF == textField) {
        
        self.verCodeView.backgroundColor = self.grayColor;
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSInteger numberSize = 11;
    if (textField == self.numberTF) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            
            return YES;
            
        } else if (self.numberTF.text.length >= numberSize) {
            
            self.numberTF.text = [textField.text substringToIndex:numberSize];
            
            return NO;
        }
    }
    
    return YES;
}

#pragma mark - event actions

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    // 背景监听键盘回收
    if (![self.numberTF isExclusiveTouch]) [self.numberTF resignFirstResponder];
    if (![self.passwordTF isExclusiveTouch]) [self.passwordTF resignFirstResponder];
    if (![self.verCodeTF isExclusiveTouch]) [self.verCodeTF resignFirstResponder];
}

#pragma mark - getter 懒加载

- (UIColor *)grayColor {
    
    if (_grayColor == nil) {
        
        _grayColor = [UIColor hex:@"#DEE3E7"];
    }
    
    return _grayColor;
}

@end
